import type { FilterItem } from '@/components/FilterBar/types'
import { getUserInfo, getFindDeviceTypeTree } from '@/api/eqpt/visual'
import { ref, reactive } from 'vue'

// 搜索字段封装
export const searchOptions: FilterItem[] = [
  {
    label: '设备编号：',
    prop: 'deviceCode',
    type: 'input',
    span: 4,
    inputProps: {
      clearable: true,
      placeholder: '模糊搜索'
    }
  },
  {
    label: '设备类型：',
    prop: 'typeId',
    type: 'treeSelect',
    defaultValue: [],
    span: 4,
    inputProps: {
      clearable: true,
      'default-expand-all': true,
      defaultProps: {
        label: 'typeName',
      },
      options: []
    }
  },
  {
    label: '使用部门：',
    prop: 'userDepartment',
    type: 'select',
    span: 5,
    multiple: true,
    defaultValue: [],
    inputProps: {
      'default-expand-all': true,
      options: []
    }
  },
  {
    label: '归属单位：',
    prop: 'homeUnit',
    type: 'treeSelect',
    span: 5,
    inputProps: {
      clearable: true,
      'default-expand-all': true,
      options: []
    }
  },
  {
    label: '航站楼：',
    prop: 'terminal',
    type: 'checkbox',
    span: 6,
    inputProps: {
      clearable: true,
      options: []
    }
  },
  {
    label: '末次维护时间：',
    prop: 'lastMaintenanceTime',
    type: 'datePicker',
    span: 8,
    defaultValue: [],
    inputProps: {
      clearable: true,
      type: 'daterange',
      'start-placeholder': 'YYYY/MM/DD',
      'end-placeholder': 'YYYY/MM/DD',
      'range-separator': '至',
      'value-format': 'YYYY-MM-DD'
    }
  },
  {
    label: '设备型号：',
    prop: 'equipmentType',
    type: 'input',
    span: 5,
    inputProps: {
      clearable: true,
      placeholder: '模糊搜索'
    }
  },
  {
    label: '维护执行人：',
    prop: 'maintenanceExecutor',
    type: 'input',
    span: 5,
    inputProps: {
      clearable: true,
      placeholder: '模糊搜索'
    }
  },
  {
    label: '维护内容：',
    prop: 'maintenanceContent',
    type: 'input',
    span: 6,
    inputProps: {
      clearable: true,
      placeholder: '关键词模糊搜索'
    }
  }
]

// 新增和编辑字段封装
export const dialogOptions = ref<Array<FilterItem>>([
  {
    label: '设备编号：',
    prop: 'eqptCode',
    type: 'input',
    span: 12,
    inputProps: {
      maxlength: 20
    }
  },
  {
    label: '归属单位：',
    prop: 'owner',
    type: 'treeSelect',
    span: 12,
    inputProps: {
      'default-expand-all': true,
      options: []
    },
    events: {
      change: (value: any, evt: any) => {
        const params = ref({
          groupIds: [] as any
        })
        params.value.groupIds.length = 0
        params.value.groupIds.push(value)
        getUserInfo(params.value).then((resp) => {
          const ExecutorData = ref([]) as any
          resp.data.forEach((d: any) => {
            const option = ref({
              label: d.userName,
              value: d.userName,
              id: d.userId
            })
            ExecutorData.value.push(option.value)
          })
          evt.setFieldsValue({ maintainer: '' })
          evt.setOptions('maintainer', ExecutorData.value)
        })

        const paramName = {
          pageNum: 1,
          pageSize: 5000,
          condition: {
            owners: params.value.groupIds
          }
        }
        getFindDeviceTypeTree(paramName).then((res: any) => {
          evt.setFieldsValue({ typeId: '' })
          changeChildrenObj(res.data.list)
          dialogOptions.value[5].inputProps.data = res.data.list
        })
      }
    }
  },
  {
    label: '设备型号：',
    prop: 'eqptModel',
    type: 'input',
    span: 12,
    inputProps: {
      clearable: true,
      placeholder: '',
      maxlength: 20
    }
  },
  {
    label: '设备名称：',
    prop: 'eqptName',
    type: 'input',
    span: 12,
    inputProps: {
      clearable: true,
      min: 0,
      placeholder: '',
      maxlength: 20
    }
  },
  {
    label: '使用部门：',
    prop: 'useOrgans',
    type: 'select',
    multiple: true,
    span: 12,
    inputProps: {
      max: 2,
      'default-expand-all': true,
      options: []
    }
  },
  {
    label: '设备类型：',
    prop: 'typeId',
    type: 'treeSelect',
    span: 12,
    inputProps: {
      'default-expand-all': true,
      'check-strictly':true,
      defaultProps: {
        label: 'typeName'
      },
      options: []
    },
    events:{
      change: (value: any, evt: any) => {
        console.log(value)
        
      }
    }
  },
  {
    label: '维护内容：',
    prop: 'file',
    type: 'input',
    span: 12,
    inputProps: {
      min: 0,
      placeholder: '',
      maxlength: 20
    }
  },
  {
    label: '维护执行人：',
    prop: 'maintainer',
    type: 'select',
    span: 12,
    inputProps: {
      options: []
    }
  },
  {
    label: '末次维护时间：',
    prop: 'lastMaintenanceTime',
    type: 'datePicker',
    span: 12,
    inputProps: {
      type: 'datetime',
      placeholder: 'YYYY-MM-DD hh:mm:ss',
      format: 'YYYY-MM-DD HH:mm:ss'
    }
  },

  {
    label: '维护周期：',
    prop: 'maintenanceCycle',
    type: 'inputNumber',
    span: 9,
    isDisabled: false,
    inputProps: {
      clearable: true,
      min: 1,
      placeholder: ''
    }
  },
  {
    label: '',
    prop: 'save',
    type: 'select',
    span: 3,
    isDisabled: false,
    inputProps: {
      options: [
        { label: '年', value: '年' },
        { label: '月', value: '月' },
        { label: '季度', value: '季度' },
        { label: '周', value: '周' },
        { label: '日', value: '日' }
      ]
    }
  },
  {
    label: '航站楼：',
    prop: 'terminal',
    type: 'radioButton',
    span: 12,
    inputProps: {
      options: [
        { label: 'T3', value: 'T3' },
        { label: 'T5', value: 'T5' }
      ]
    }
  },

  {
    label: '楼层：',
    prop: 'floor',
    type: 'select',
    span: 12,
    inputProps: {
      options: [
        {
          lable: '1楼',
          value: '1楼'
        },
        {
          lable: '2楼',
          value: '2楼'
        },
        {
          lable: '3楼',
          value: '3楼'
        }
      ]
    }
  },

  {
    label: '位置：',
    prop: 'position',
    type: 'input',
    span: 12,
    inputProps: {
      maxlength: 20,
      options: []
    }
  },
  {
    label: '地图坐标：',
    prop: 'coordinate',
    type: 'inputBtn',
    isDisabled: true,
    span: 12,
    icon: 'Location',
    defaultValue: '',
    inputProps: {
      placeholder: '回显点位',
      clearable: true
    },
    events: {
      click: () => {}
    }
  }
])

// 记录设施设备维护内容字段封装
export const repairDialogOptions = ref<Array<FilterItem>>([
  {
    label: '设备编号：',
    prop: 'repairDeviceCode',
    type: 'input',
    span: 12,
    isDisabled: true,
    inputProps: {
      clearable: true,
      disabled: true
    }
  },
  {
    label: '设备类型：',
    prop: 'repairDeviceType',
    type: 'select',
    span: 12,
    isDisabled: true,
    inputProps: {
      options: [
        {
          label: '',
          value: ''
        }
      ]
    }
  },
  {
    label: '维护内容：',
    prop: 'repairMaintenanceContent',
    type: 'input',
    span: 12,
    inputProps: {
      clearable: true,
      placeholder: ''
    }
  },
  {
    label: '末次维护时间：',
    prop: 'repairLastMaintenanceTime',
    type: 'datePicker',
    span: 12,
    defaultValue: [],
    inputProps: {
      type: 'datetime',
      'value-format': 'YYYY-MM-DD HH:mm:ss',
      format: 'YYYY-MM-DD HH:mm:ss'
    }
  },
  {
    label: '维护执行人：',
    prop: 'repairMaintenanceExecutor',
    type: 'select',
    span: 12,
    inputProps: {
      placeholder: '请选择',
      options: []
    }
  },
  {
    label: '',
    prop: 'repair',
    type: 'select',
    span: 12,
    inputProps: {
      clearable: true,
      min: 0,
      placeholder: ''
    }
  },
  {
    label: '附件：',
    prop: 'repairAttachments',
    type: 'upload',
    span: 12,
    inputProps: {
      clearable: true,
      min: 0,
      placeholder: ''
    }
  }
])

// 维护提醒字段封装
export const maintenanceTipOptions: FilterItem[] = [
  {
    label: '在维保期限前',
    prop: 'time',
    type: 'inputNumber',
    span: 16,
    isDisabled: false,
    inputProps: {
      clearable: true,
      min: 1,
      placeholder: ''
    }
  },
  {
    label: '',
    prop: 'unit',
    type: 'select',
    span: 4,
    isDisabled: false,
    inputProps: {
      placeholder: '请选择',
      options: [
        { label: '年', value: '年' },
        { label: '月', value: '月' },
        { label: '季度', value: '季度' },
        { label: '周', value: '周' },
        { label: '日', value: '日' }
      ]
    }
  }
]

export function changeChildrenObj(records:any[]){
  records.forEach(item => {    
    item.value = item.typeId
    if(item.children.length > 0){
      changeChildrenObj(item.children)
    }
  })
}